package com.example.course.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.course.dto.TeacherDTO;
import com.example.course.dto.TeacherLoginDTO;
import com.example.course.dto.TeacherQueryDTO;
import com.example.course.service.TeacherService;
import com.example.course.tool.R;
import com.example.course.vo.TeacherVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 教师控制器，处理与教师相关的HTTP请求
 *
 * @author Jasmine-drs
 * @date 2025/4/23 11:30
 */
@Slf4j
@RestController
@RequestMapping("/teachers")
@AllArgsConstructor
@Api(tags = "教师管理接口")
public class TeacherController {
	private final TeacherService teacherService;

	/**
	 * 教师登录
	 *
	 * @param loginDTO 登录信息(手机号和密码)
	 * @return 登录成功返回教师信息和token
	 */
	@PostMapping("/login")
	@ApiOperation("教师登录")
	public R<TeacherVO> login(@ApiParam(value = "登录信息", required = true) @Valid @RequestBody TeacherLoginDTO loginDTO) {
		log.info("教师登录请求：{}", loginDTO.getPhone());

		try {
			// 调用服务层处理登录
			TeacherVO teacherVO = teacherService.login(loginDTO);
			return R.success(teacherVO);
		} catch (IllegalArgumentException e) {
			log.warn("教师登录参数异常", e);
			return R.err(e.getMessage());
		} catch (Exception e) {
			log.error("教师登录异常", e);
			return R.err("登录失败：" + e.getMessage());
		}
	}

	/**
	 * 教师注册
	 *
	 * @param teacherDTO 教师信息
	 * @return 注册成功返回教师信息
	 */
	@PostMapping("/register")
	@ApiOperation("教师注册")
	public R<TeacherVO> register(@ApiParam(value = "教师信息", required = true) @Valid @RequestBody TeacherDTO teacherDTO) {
		log.info("教师注册请求：{}", teacherDTO);

		try {
			// 调用服务层处理注册
			TeacherVO teacherVO = teacherService.register(teacherDTO);
			return R.success(teacherVO);
		} catch (IllegalArgumentException e) {
			log.warn("教师注册参数异常", e);
			return R.err(e.getMessage());
		} catch (Exception e) {
			log.error("教师注册异常", e);
			return R.err("注册失败：" + e.getMessage());
		}
	}

	/**
	 * 获取教师列表
	 *
	 * @param queryDTO 查询条件
	 * @return 教师列表
	 */
	@GetMapping
	@ApiOperation("获取教师列表")
	public R<Page<TeacherVO>> getTeacherList(@Valid TeacherQueryDTO queryDTO) {
		log.info("请求获取教师列表：{}", queryDTO);

		try {
			// 调用服务层获取教师列表
			Page<TeacherVO> page = teacherService.getTeacherList(queryDTO);
			return R.success(page);
		} catch (Exception e) {
			log.error("获取教师列表异常", e);
			return R.err("获取教师列表失败：" + e.getMessage());
		}
	}

	/**
	 * 获取教师详情
	 *
	 * @param id 教师ID
	 * @return 教师详情
	 */
	@GetMapping("/{id}")
	@ApiOperation("获取教师详情")
	public R<TeacherVO> getTeacherDetail(@ApiParam(value = "教师ID", required = true) @PathVariable Integer id) {
		log.info("请求获取教师详情，ID：{}", id);

		try {
			// 调用服务层获取教师详情
			TeacherVO teacherVO = teacherService.getTeacherById(id);
			return R.success(teacherVO);
		} catch (IllegalArgumentException e) {
			log.warn("获取教师详情参数异常", e);
			return R.err(e.getMessage());
		} catch (Exception e) {
			log.error("获取教师详情异常", e);
			return R.err("获取教师详情失败：" + e.getMessage());
		}
	}
} 